package com.example.shiwuyouproject.ui.fragment.my.activity

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityMyincomeBinding
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.MyIncomeViewModel
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.MyIncomeAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.CommissionTotalBean
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.GetUserShareDataBean
import com.example.shiwuyouproject.ui.fragment.my.bean.GetCommentListsBean
import com.example.shiwuyouproject.ui.my.bean.MyIncomeBean
import com.example.shiwuyouproject.utils.ActStartUtils
import com.example.shiwuyouproject.utils.MyTimeUtils
import com.example.shiwuyouproject.utils.ShareUtils
import com.yechaoa.yutilskt.YUtils
import java.util.*

/**
 * 我的收益
 */
class MyIncomeActivity : BaseVmActivity<ActivityMyincomeBinding, MyIncomeViewModel>() {
    override fun viewModelClass(): Class<MyIncomeViewModel> = MyIncomeViewModel::class.java

    override fun getViewBinding(): ActivityMyincomeBinding {
        return ActivityMyincomeBinding.inflate(layoutInflater)
    }

    private var headerView: View? = null
    private var create_year: String = ""//年
    private var create_month: String = ""//月
    private var share_img=""
    private var page = 1
    override fun initView() {
        mBinding.myTitleBar.tvTitle.text = "我的收益"
        share_img=intent.getStringExtra("share_img").toString()
        headerView = LayoutInflater.from(this).inflate(R.layout.view_myincome, null)
        create_year = MyTimeUtils.getYear().toString()
        create_month = MyTimeUtils.getMonth().toString()
        mBinding.tvYear.text = create_year + "年"
        if (create_month.toInt() < 10) {
            mBinding.tvMoth.text = "0" + create_month + "月"
        } else {
            mBinding.tvMoth.text = create_month + "月"
        }
        mViewModel.getCommissionTotal(create_year, create_month)
        mViewModel.PostCommissionLists(page.toString(), create_year, create_month)
        mViewModel.getUserShareData()
    }

    private var myMyIncomeAdapter: MyIncomeAdapter? = null
    override fun initData() {
        myMyIncomeAdapter = MyIncomeAdapter(this)
        mBinding.myRecyclerView.adapter = myMyIncomeAdapter
        val mview = LayoutInflater.from(this).inflate(R.layout.view_nodata, null)
        myMyIncomeAdapter?.emptyView = mview
        initCustomTimePicker()
    }

    override fun setListener() {
        mBinding.clYear.setOnClickListener { //选择时间
            pvCustomLunar?.show()
        }
        mBinding.myTitleBar.tvBack.setOnClickListener { finish() }
        mBinding.mySmartRefreshLayout.setOnRefreshListener {
            page = 1
            mViewModel.getCommissionTotal(create_year, create_month)
            mViewModel.PostCommissionLists(page.toString(), create_year, create_month)
        }
        mBinding.mySmartRefreshLayout.setOnLoadMoreListener {
            page++
            mViewModel.getCommissionTotal(create_year, create_month)
            mViewModel.PostCommissionLists(page.toString(), create_year, create_month)
        }
        mBinding.tvMyInvitation.setOnClickListener {
            ActStartUtils.startAct(this, MyInvitationActivity::class.java)
        }
        mBinding.tvInviteNow.setOnClickListener {//分享
                if(getUserShareDataBean!=null){
                                ShareUtils.onRevenueInvitationShare(getUserShareDataBean?.share_title!!,
                                        getUserShareDataBean?.share_link!!,
                                        getUserShareDataBean?.share_img!!,
                                        getUserShareDataBean?.share_content!!)
                }

        }
    }

    var pvCustomLunar: TimePickerView? = null
    var cuurDataTime: Date? = null
    private fun initCustomTimePicker() {
        val selectedDate = Calendar.getInstance() //系统当前时间
        val startDate = Calendar.getInstance()
        startDate[1990, 1] = 23
        val endDate = Calendar.getInstance()
        endDate[2069, 2] = 28
        //时间选择器 ，自定义布局
        //时间选择器 ，自定义布局
        pvCustomLunar = TimePickerBuilder(
            this
        ) { date, v -> //选中事件回调
            cuurDataTime = date
        }
            .setDate(selectedDate)
            .setRangDate(startDate, endDate)
            .setLayoutRes(
                R.layout.pickerview_custom_lunar
            ) { v ->
                val tvSubmit = v.findViewById<View>(R.id.tv_finish) as TextView
                val ivCancel = v.findViewById<View>(R.id.iv_cancel) as TextView
                tvSubmit.setOnClickListener {
                    pvCustomLunar?.returnData()
                    pvCustomLunar?.dismiss()
                    create_year = cuurDataTime?.let { it1 -> MyTimeUtils.getYear(it1) }.toString()
                    mBinding.tvYear.text = create_year + "年"
                    create_month = cuurDataTime?.let { it1 -> MyTimeUtils.getMoth(it1) }.toString()
                    mBinding.tvMoth.text = create_month + "月"
                    YUtils.showLoading(this, "查询中...")
                    mViewModel.getCommissionTotal(create_year, create_month)
                    mViewModel.PostCommissionLists(page.toString(), create_year, create_month)
                }
                ivCancel.setOnClickListener { pvCustomLunar?.dismiss() }
            }
            .setDividerColor(Color.WHITE)//设置分割线的颜色
            .setType(booleanArrayOf(true, true, false, false, false, false))
            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .setDividerColor(Color.RED)
            .build()
    }
    private  var commissionTotalBean: CommissionTotalBean?=null
    private var getUserShareDataBean: GetUserShareDataBean?=null
    override fun observe() {
        mViewModel.messageStatus.observe(this, androidx.lifecycle.Observer<String>{

        })
        mViewModel.getCommissionTotalResult.observe(this,androidx.lifecycle.Observer<CommissionTotalBean> {
            mBinding.tvMoney.text = it.data.count
            mBinding.tvShouYiMoney.text = it.data.subtotal
        })
        mViewModel.PostCommissionListsResult.observe(this, androidx.lifecycle.Observer<MyIncomeBean>{
            YUtils.hideLoading()
            if (page == 1) {
                mBinding.mySmartRefreshLayout.finishRefresh()
                myMyIncomeAdapter?.setNewData(it.data.data)
            } else {
                myMyIncomeAdapter?.addData(it.data.data)
                mBinding.mySmartRefreshLayout.finishLoadMore()
            }
            if (page <= it.data.total) {
                mBinding.mySmartRefreshLayout.finishLoadMoreWithNoMoreData()
            }
            if (myMyIncomeAdapter?.data?.size == 0) {//显示无数据样式
            }
        })
        mViewModel.getUserShareDataResult.observe(this,androidx.lifecycle.Observer<GetUserShareDataBean>{
            getUserShareDataBean=it

        })
    }
}